package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.AdminAreaBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.secret.SecretReqUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.Map;

/**
 * 结构化地址-修改
 *
 * @author 甄玉晴
 */
public class AdminAreaUpdate implements ServerInterface {

    private static Logger logger = LogManager.getLogger(AdminAreaUpdate.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
        	PubAdminAreaMapper mapper = factory.getMapper(PubAdminAreaMapper.class);
            AdminAreaBean bean = JsonUtils.objectToPojo(req.getData(), AdminAreaBean.class);
            if(bean==null||StringUtils.isEmpty(bean.getId())) {
            	MessageBean.create(Constant.ILLEGAL_PARAMETER, "缺少主键id", void.class);
            }
            bean.setUpdateInfo(userInfo);
            //更新主数据
            Map<String,Object> params=new HashMap<String,Object>();
            params.put("modifyBy",bean.getModifyBy());
            params.put("modifyTime", bean.getModifyTime());
            params.put("id", bean.getId());
            params.put("latitude", bean.getLatitude());
            params.put("longitude", bean.getLongitude());
            params.put("sname", bean.getSname());
            params.put("status", bean.getStatus());
            Map<String, Object> map = buildParams("pub_admin_area", params);
            //设置主键名称
            map.put("primaryKey","ID");
            //MessageBean<?> mobileCustomer = getMobileCustomer(StaticInfo.midMobileCustomer+"/masterData/update", map);
            //if (mobileCustomer.getCode() == Constant.MESSAGE_INT_FAIL){
                //return mobileCustomer;
            //}
            // 2、更新本地数据
            mapper.updateArea(bean);
        } catch (Exception e) {
            logger.error("结构地址修改失败", e);
            return MessageBean.create(Constant.ILLEGAL_PARAMETER, "结构地址修改失败", void.class);
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }
    private String getSonAreaNo(SessionFactory factory,String areaNo){
        PubAdminAreaMapper mapper = factory.getMapper(PubAdminAreaMapper.class);
        String maxAreaNo = mapper.getMaxAreaNo(areaNo,areaNo.length() + 5);
        if (maxAreaNo != null){
            int i = Integer.parseInt(maxAreaNo.substring(maxAreaNo.length() - 5)) + 1;
             String tmp = String.format("%05d",i);
            return areaNo + tmp;
        }else {
            return areaNo+"00001";
        }
    }
    private String nameById(SessionFactory factory,String parentId,String sname){
        PubAdminAreaMapper mapper = factory.getMapper(PubAdminAreaMapper.class);
        StringBuilder strName = new StringBuilder();
        String pName = mapper.getNameById(parentId);
        if(StringUtils.isEmpty(pName)){
            strName.append(mapper.getNameById(parentId) + sname);
        }else {
            strName.append(sname);
        }
        return strName.toString();
    }
    private MessageBean<?> getMobileCustomer(String url, Object data){
    	com.koron.util.secret.RequestBean reqBean = new com.koron.util.secret.RequestBean();
        reqBean.setUrl(url);
        reqBean.setData(data);
        String result = null;
        try {
            result = SecretReqUtil.sendToGatWay(reqBean);
        }catch (Exception e){
            logger.error("调用中台移动客服失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,e.getMessage(),void.class);
        }
        MessageBean<String> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"操作成功",String.class);
        info.setData(result);
        return info;
    }
    private Map<String,Object> buildParams(String tableName,Map<String,Object> params){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> sendParam = new HashMap<>();
        for (String key : params.keySet()){
            String s = changeColumn(key);
            if (s != null && !"".equals(s)){
                sendParam.put(s,params.get(key));
            }
        }
        map.put("tableName",tableName);
        map.put("params",sendParam);
        return map;
    }
    private String changeColumn(String columns){
        Map<String,String> map = new HashMap<>();
        map.put("id","ID");
        map.put("sname","SNAME");
        map.put("parentId","PARENTID");
        map.put("isLeaf","ISLEAF");
        map.put("status","STATUS");
        map.put("createBy","CREATEBY");
        map.put("createTime","CREATETIME");
        map.put("modifyBy","MODIFYBY");
        map.put("modifyTime","MODIFYTIME");
        map.put("longitude","LONGITUDE");
        map.put("latitude","LATITUDE");
        map.put("areaNo","AREA_NO");
        map.put("areaName","AREA_NAME");
        map.put("fullName","FULL_NAME");
        return map.get(columns);
    }

}